package org.example.maven.museumuser.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.maven.museumuser.domain.entity.IdentityName;
import org.example.maven.museumuser.mapper.IdentityNameMapper;
import org.example.maven.museumuser.service.IdentityNameService;
import org.springframework.stereotype.Service;

@Service
public class IdentityNameServiceImpl extends ServiceImpl<IdentityNameMapper, IdentityName> implements IdentityNameService {
    @Override
    public boolean addIdentityByName(IdentityName entity) {
        // 根据 identity 查询现有记录
        IdentityName existing = this.getOne(new QueryWrapper<IdentityName>()
                .eq("identity", entity.getIdentity()));

        if (existing != null) {
            // 如果存在且需要更新名称
            if (!StringUtils.equals(existing.getName(), entity.getName())) {
                existing.setName(entity.getName());
                return this.updateById(existing);
            }
            return true;
        }
        return this.save(entity);
    }
}
